//
//  HotTopicTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/1.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "HotTopicTableViewCell.h"
#import "HotTopicCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfHotTopic.h"
@interface HotTopicTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrayOfHotTopic;
@end
@implementation HotTopicTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.layout.minimumLineSpacing = 10;//横向间距| || |
        self.layout.minimumInteritemSpacing = 0;
                self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.bounces = NO;
        [self.contentView addSubview:self.collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[HotTopicCollectionViewCell class] forCellWithReuseIdentifier:@"HotTopicCollectionViewCell"];
    }
    return self;
}
- (void)handleData
{
    _arrayOfHotTopic = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=index&op=special_list_1_5" par:nil success:^(id responseObject) {
        NSDictionary *dictionary = [responseObject objectForKey:@"datas"];
        NSArray *array = dictionary[@"hot_topics"];
        for (NSDictionary *dic in array) {
            ModelOfHotTopic *model = [[ModelOfHotTopic alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfHotTopic addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self layoutIfNeeded];
    self.layout.itemSize = CGSizeMake(self.contentView.bounds.size.width - 180, self.contentView.bounds.size.height);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfHotTopic.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotTopicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotTopicCollectionViewCell" forIndexPath:indexPath];
    ModelOfHotTopic *model = [_arrayOfHotTopic objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.class_image]];
    cell.labelOfTopic.text = model.class_name;
    cell.labelOfTopic.textAlignment = 1;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfHotTopic *model = [_arrayOfHotTopic objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(hotTopicWithID:)]) {
        [self.delegate hotTopicWithID:model.class_id];
    }
    
}
@end
